package com.zaoniao.enstudy.infrastructure.persistence;

import com.github.pagehelper.PageHelper;
import com.zaoniao.enstudy.domain.entity.Vocabulary;
import com.zaoniao.enstudy.domain.repository.UserVocabularyRepository;
import com.zaoniao.enstudy.infracore.enums.YesOrNo;
import com.zaoniao.enstudy.infracore.web.PagedGridResult;
import com.zaoniao.enstudy.infrastructure.convert.UserVocabularyCrt;
import com.zaoniao.enstudy.infrastructure.mapper.UserVocabularyMapper;
import com.zaoniao.enstudy.infrastructure.po.UserVocabularyPo;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * 词汇表仓储层业务实现类.
 *
 * @author llb666
 */
@Repository
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class UserVocabularyRepositoryImpl implements UserVocabularyRepository {

    private final UserVocabularyMapper userVocabularyMapper;

    /**
     * 新增词汇表
     *
     * @param vocabulary 词汇表
     */
    @Override
    public Vocabulary insert(Vocabulary vocabulary) {
        UserVocabularyPo userVocabularyPo = UserVocabularyCrt.INSTANCE.do2po(vocabulary);
        userVocabularyPo.setCreateUser(vocabulary.getUserId());
        userVocabularyPo.setUpdateUser(vocabulary.getUserId());
        userVocabularyPo.setStatus(YesOrNo.YES.type);
        userVocabularyMapper.insertUseGeneratedKeys(userVocabularyPo);
        return UserVocabularyCrt.INSTANCE.po2do(userVocabularyPo);
    }

    /**
     * 修改词汇表名称.
     *
     * @param vocabularyId   词汇表Id
     * @param vocabularyName 词汇表名称
     */
    @Override
    public void updUserVocabulary(Integer vocabularyId, String vocabularyName) {
        UserVocabularyPo userVocabularyPo = new UserVocabularyPo();
        userVocabularyPo.setId(vocabularyId);
        userVocabularyPo.setVocabularyName(vocabularyName);
        userVocabularyMapper.updateByPrimaryKeySelective(userVocabularyPo);
    }

    /**
     * 根据主键删除词汇表.
     *
     * @param vocabularyId 词汇表Id
     */
    @Override
    public void delUserVocabulary(Integer vocabularyId) {
        UserVocabularyPo userVocabularyPo = new UserVocabularyPo();
        userVocabularyPo.setId(vocabularyId);
        userVocabularyPo.setStatus(YesOrNo.NO.type);
        userVocabularyMapper.updateByPrimaryKeySelective(userVocabularyPo);
    }

    /**
     * 词汇表分页查询.
     *
     * @param userId 用户id
     * @param page   第几页
     * @param size   每页显示数量
     * @return 词汇表列表
     */
    @Override
    public PagedGridResult<Vocabulary> getVocabularyPage(String userId, Integer page, Integer size) {
        Example example = new Example(UserVocabularyPo.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("userId", userId);

        PageHelper.startPage(page, size);
        List<UserVocabularyPo> userVocabularyPos = userVocabularyMapper.selectByExample(example);
        PagedGridResult<UserVocabularyPo> pagedGridResult = PagedGridResult.setterPagedGrid(userVocabularyPos);
        return UserVocabularyCrt.INSTANCE.po2doPage(pagedGridResult);
    }

    /**
     * 判断当前用户是否存在同名词汇表
     *
     * @param vocabulary 词汇表信息
     * @return true/false
     */
    @Override
    public boolean checkName(Vocabulary vocabulary) {
        Example example = new Example(UserVocabularyPo.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("userId", vocabulary.getUserId());
        criteria.andEqualTo("vocabularyName", vocabulary.getVocabularyName());
        int count = userVocabularyMapper.selectCountByExample(example);
        return count > 0;
    }

    /**
     * 检查当前用户是否存在词汇表.
     *
     * @param userId 用户Id
     * @return true/false
     */
    @Override
    public boolean checkCountByUserId(String userId) {
        Example example = new Example(UserVocabularyPo.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("userId", userId);
        int count = userVocabularyMapper.selectCountByExample(example);
        return count > 0;
    }

}
